package com.example.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.example.dao2.PeopleDao;
import com.example.entity.People;
import com.example.utils.DButil;

public class PeopleDaoImpl implements PeopleDao{

	public People getPeopleById(String idno) {
	 String sql="select id,name,age,gender,address,crimeRecord,idno,pic from EscapedPeopleTbl where idno=?";
	 DButil db=new  DButil();
	 Connection conn=db.openConnection();
	 
	 try {
		PreparedStatement pstmt=conn.prepareStatement(sql);
		pstmt.setString(1, idno);
		ResultSet rs=pstmt.executeQuery();
		if (rs.next()) {
			//获得人口信息
			String id=rs.getString(1);
			String name=rs.getString(2);
			int age=rs.getInt(3);
			String gender=rs.getString(4);
			String address=rs.getString(5);
			String crimeRecord=rs.getString(6);
			String pic=rs.getString(8);
			People p=new People();
			p.setId(id);
			p.setName(name);
			p.setAge(age);
			p.setGender(gender);
			p.setAddress(address);
			p.setCrimeRecord(crimeRecord);
			p.setIdno(idno);
			p.setPic(pic);
			return p;
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		db.closeConn(conn);
	}
	 
		return null;
	}

	public People getPeopleByName(String name) {
		String sql="select id,name,age,gender,address,crimeRecord,idno,pic from EscapedPeopleTbl where name=?";
		 DButil db=new  DButil();
		 Connection conn=db.openConnection();
		 
		 try {
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, name);
			ResultSet rs=pstmt.executeQuery();
			if (rs.next()) {
				//获得人口信息
				String id=rs.getString(1);
				//String name=rs.getString(2);
				int age=rs.getInt(3);
				String gender=rs.getString(4);
				String address=rs.getString(5);
				String crimeRecord=rs.getString(6);
				String pic=rs.getString(8);
				People p=new People();
				p.setId(id);
				p.setName(name);
				p.setAge(age);
				p.setGender(gender);
				p.setAddress(address);
				p.setCrimeRecord(crimeRecord);
				p.setIdno(id);
				p.setPic(pic);
				return p;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		 
			return null;
	}

}
